package gui;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

public class PanelFactory {

    private static final ConcurrentHashMap<String, BasePanel> panels = new ConcurrentHashMap<>();

    public static BasePanel getPanel(Class<? extends BasePanel> panelType, boolean noCache) {
        String typeName = panelType.getSimpleName();
        if (!noCache && panels.containsKey(typeName)) {
            return panels.get(typeName);
        } else {
            try {
                BasePanel panel = panelType.newInstance();

                if (!noCache) {
                    panels.put(typeName, panel);
                }
                return panel;
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static BasePanel getPanel(Class<? extends BasePanel> panelType) {
        BasePanel panel = getPanel(panelType, false);
        if (panel != null) {
            panel.clearAllInput();
        }
        return panel;
    }

}
